#include "error.h"
#include "lexer.h"
namespace Lexer
{
    Token_value curr_tok = PRINT;
    double number_value;
    string string_value;
    map<string,double> table;
    extern istream* input;
}
/*get a token from the input stream, and the global var
  cur_tok indicates the token's type*/
Lexer::Token_value Lexer::get_token()
{
    char ch = 0;
    do{//skip the space
        if(!input->get(ch)) return curr_tok = END;
    }while(ch != '\n' && isspace(ch));
    
    switch(ch){
    case ';':
    case '\n':
        return curr_tok = PRINT;//return and ...
    case '*':
    case '/':
    case '+':
    case '-':
    case '(':
    case ')':
    case '=':
        return curr_tok = Token_value(ch);
    case '0':case'1':case '2': case '3':case '4':
    case '5':case '6':case '7':case '8':case '9':
    case '.':
        input->putback(ch);
        (*input)>> number_value;
        return curr_tok = NUMBER;
    default:
        if(isalpha(ch)){
            input->putback(ch);
            (*input) >> string_value;
            return curr_tok = NAME;
        }
        Error::error("bad token");
        return curr_tok = PRINT;
    }
}

